package com.maoye.ossp.biz.intent.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hotent.base.core.util.string.StringUtil;
import com.maoye.ossp.biz.contract.entity.ContractTemplate;
import com.maoye.ossp.biz.contract.service.IAdvertAssitemsService;
import com.maoye.ossp.biz.contract.service.IAdvertAssociatedService;
import com.maoye.ossp.biz.contract.service.IAssbaseitemsService;
import com.maoye.ossp.biz.contract.service.IAssitemsService;
import com.maoye.ossp.biz.contract.service.IContractService;
import com.maoye.ossp.biz.contract.service.ILeaseAssContractService;
import com.maoye.ossp.biz.contract.service.ILeaseAssbaseitemsService;
import com.maoye.ossp.biz.contract.service.ILeaseAssitemsService;
import com.maoye.ossp.biz.contract.service.IMarketAssbaseitemsService;
import com.maoye.ossp.biz.contract.service.IMarketAssitemsService;
import com.maoye.ossp.biz.contract.service.IMarketAssociatedService;
import com.maoye.ossp.biz.contract.service.ISelfAssitemsService;
import com.maoye.ossp.biz.contract.service.ISelfAssociatedService;
import com.maoye.ossp.biz.contract.service.TemplateService;
import com.maoye.ossp.biz.contract.utils.ContractUtils;
import com.maoye.ossp.biz.contract.utils.JacksonUtils;
import com.maoye.ossp.biz.intent.entity.IntAssbrands;
import com.maoye.ossp.biz.intent.entity.IntAssociated;
import com.maoye.ossp.biz.intent.entity.IntAttachment;
import com.maoye.ossp.biz.intent.entity.IntCreatePdfEntity;
import com.maoye.ossp.biz.intent.service.IIntAssociatedService;
import com.maoye.ossp.biz.intent.service.IIntAssterminateService;
import com.maoye.ossp.biz.intent.service.IIntCreatePDFService;
import com.maoye.ossp.biz.intent.service.IntAttachmentService;
import com.maoye.ossp.biz.negotiation.entity.Assrecords;
import com.maoye.ossp.biz.negotiation.entity.NegAssTempItemCode;
import com.maoye.ossp.biz.negotiation.entity.NegAssbaseitems;
import com.maoye.ossp.biz.negotiation.entity.NegAssbrands;
import com.maoye.ossp.biz.negotiation.entity.NegAssitems;
import com.maoye.ossp.biz.negotiation.entity.NegAssshops;
import com.maoye.ossp.biz.negotiation.service.AssinfoService;
import com.maoye.ossp.biz.negotiation.service.AssrecordService;
import com.maoye.ossp.biz.negotiation.util.BuildEntity;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.att.service.IAttService;
import com.maoye.ossp.sys.att.util.FileUtils;
import com.maoye.ossp.sys.base.entity.CMCate;
import com.maoye.ossp.sys.base.service.ICMCateService;
import com.maoye.ossp.sys.base.service.SysBaseFormatService;
import com.maoye.ossp.sys.base.utils.SysDictUtils;
import com.maoye.ossp.sys.msg.entity.Message;
import com.maoye.ossp.sys.msg.entity.TodoMessage;
import com.maoye.ossp.sys.msg.handler.MsgStrategyContext;
import com.maoye.ossp.sys.msg.util.MessageConstant;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.sc.api.domain.Worker;
import com.maoye.sc.api.local.UserService;
import com.maoye.sc.service.user.UserServiceImpl;
import com.maoye.sc.service.user.WorkerServiceImpl;

/**
 * 对象功能:联营撤柜意向  控制器类
 * 开发公司:MOI
 * 开发人员:zhongjt
 * 创建时间:2014-02-20 14:50:38
 */
@Controller
@RequestMapping("biz/intent/common")
public class IntCommController extends BaseController {
	@Resource
	private IIntAssterminateService intAssterminateService;
	@Resource
	private IContractService  contractServce;
	
	@Resource
	private TemplateService templateService;
	@Resource
	private IIntAssociatedService intAssociatedService;
	
	@Resource
	private IIntCreatePDFService intcreatePDFService;
	
	@Resource
	SysBaseFormatService sysBaseFormatService;
	@Resource
	IAttService attService;
	@Resource	
	AssinfoService assinfoService;
	@Resource
	AssrecordService assrecordService;
	@Resource
	private ICMCateService icmCateService;		//招商分类
	@Resource 
	FileUtils fileUtils;
	
	@Resource
	WorkerServiceImpl   workerServiceImpl;
	@Resource
	UserService userService;
	@Resource
	MsgStrategyContext mscServicec;
	@Resource
	ILeaseAssContractService  leaseService; //租赁合同
	@Resource
	IAdvertAssociatedService advertService;   // 广告合同

	@Resource
	private UserServiceImpl  userServiceImpl;
	@Resource
	MsgStrategyContext messageService;

	@Resource
	IMarketAssociatedService marketService;  //促销合同
	@Resource
	ISelfAssociatedService selfService;    //自营合同
	@Resource
	IntAttachmentService attServie;
	
	@Resource
	IAssbaseitemsService assbaseService;
	@Resource
	IAssitemsService assiteseService;
	@Resource
	ISelfAssitemsService selfitemService;
	@Resource
	ILeaseAssitemsService leaseAssitemsService;
	@Resource
	ILeaseAssbaseitemsService leaseAssbaseitemsService;
	@Resource
	IMarketAssbaseitemsService marketAssbaseitemsService;
	@Resource
	IMarketAssitemsService   marketAssitemsService;
	@Resource
	IAdvertAssitemsService advertAssitemsService;
	
	
	
	private void sendMessagePur(IntAssociated intAssociated,String navId) throws Exception {
		List<String> targets = Lists.newArrayList();
		targets.add(intAssociated.getSupplierId().toString());
		TodoMessage toMessage = new TodoMessage();
		toMessage.setId(idGenerator.getUId());
		toMessage.setUrl(IntCommController.getReturnPage(intAssociated.getContractType(), navId, intAssociated.getId().toString()));   
		toMessage.setTitle("尊敬的供应商朋友，您好！意向单号"+intAssociated.getIntentNo()+"合同文本已经生成，请查阅无误之后点击下载，谢谢");
		toMessage.setTitleLink(IntCommController.getReturnPage(intAssociated.getContractType(), navId, intAssociated.getId().toString()));
		toMessage.setSource(MessageConstant.SOURCE_OSSP);
		toMessage.setMessageType(MessageConstant.TODO_TYPE_NOTIFY);
		toMessage.setContent(intAssociated.getSubject()+"附件已生成");
		toMessage.setContentTypeFlag(MessageConstant.TODO_CONTENT_TYPE_TEXT);
		toMessage.setCreateBy(UserUtils.getCurrentUserId().toString());
		toMessage.setCreateDate(new Date());
		toMessage.setPersonIds(targets);
		messageService.send(toMessage);
	}
	
	/**
	 * 通知供应商下载合同
	 * @param id
	 * @param model
	 * @return
	 * @author yanghao
	 * @throws Exception 
	 * @data 2014年5月23日
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "{id}/sendMessage")
	public void sendMessage(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response, Model model) throws Exception {
		IntAssociated intAssociated = intAssociatedService.get(id);
		String navId=request.getParameter("navId");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		StringBuilder sb = new StringBuilder();
		sendMessagePur(intAssociated,navId);
		sb.append("{\"success\":true}");
		out.write(sb.toString());
		out.flush();
		out.close();
	}
	
	
	/**
	 * 获取合同文本模板
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author yanghao
	 * @data 2014年6月5日
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "getTemplate")
	public String getTemplate(HttpServletRequest request,HttpServletResponse response,Model model){
		Map map = new HashMap();
		String intentType = request.getParameter("intentType");
		List<ContractTemplate> templateList = templateService.getList(map);
		model.addAttribute("templateList", templateList);
		return resultPage("/biz/intent/common/templateModal", model, request);
	}
	/**
	 * 生成文本
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @author yanghao
	 * @throws IOException 
	 * @data 2014年6月5日
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "{templateId}/{intentId}/createText")
	public String createText(HttpServletRequest request,@PathVariable String intentId,@PathVariable String templateId,HttpServletResponse response,RedirectAttributes redirectAttributes,Model model) throws Exception{
		IntAssociated  intAssociated  = intAssociatedService.get(Long.parseLong(intentId));
		String navId = request.getParameter("navId");
		String intentType = request.getParameter("intentType");
		Map<String, Object> map = null;
		IntCreatePdfEntity pdfEntity = intcreatePDFService.initPdfEntity(Long.parseLong(intentId));
		ContractTemplate temp = templateService.get(Long.parseLong(templateId));
		String pdfName = temp.getTemplateName()+"-"+intAssociated.getSubject()+"("+intAssociated.getIntentNo()+")";
		map =templateService.createPdf(Long.parseLong(templateId), intentId, pdfName, pdfEntity, request);
		intAssociated.setIsContractModify(ContractUtils.IS_CONTRACT_MODIFY_YES);
		intAssociatedService.updateIsContractModify(intAssociated);//是否生成合同文本设置为已生成
		addMessage(redirectAttributes, "生成'" + intAssociated.getSubject()+ "合同文本'成功");
		return "redirect:/biz/intent/"+intentType+"/"+intentId+"/view?navId="+navId;
	}
	
	/**
	 * 编辑意向品牌信息
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @param model
	 * @return
	 * @author yanghao
	 * @throws Exception 
	 * @data 2014年6月10日
	 */
	@RequestMapping(value = "editIntAssbrand")
	public String editIntAssbrand(HttpServletRequest request,HttpServletResponse response,String assBrand,String operate,String num,
			String supplierNo, String brandId,RedirectAttributes redirectAttributes,Model model) throws Exception{
		String _brand=assBrand.replace("'", "\"");
		SimpleFilterProvider fp = new SimpleFilterProvider();
		fp.addFilter("executeFilter",
				SimpleBeanPropertyFilter.serializeAllExcept(new String[] {
				 "createBy" }));
		IntAssbrands brand =JacksonUtils.create().filter("myFilter", "id").json2Obj(_brand, IntAssbrands[].class)[0];
		model.addAttribute("brand", brand);
		CMCate cmcCate = null;
		CMCate cmcCate2 = null;
		String cateName = "";
		if(brand.getInviteCate()!=null){
			cmcCate = icmCateService.getByCateNo(brand.getInviteCate().toString());
			cateName = cmcCate.getCateName();
			String cateNames = IntCommController.getCateName(cmcCate, icmCateService);	//获得招商分类名称 
			cmcCate.setCateName(cateNames);
		}
		if(brand.getPurchasingGroup()!=null){
			cmcCate2= icmCateService.getByCateNo(brand.getPurchasingGroup().toString());
		}
		model.addAttribute("cmcCate", cmcCate);
		model.addAttribute("purchasingGroup", cmcCate2);
		model.addAttribute("operate","edit");
		model.addAttribute("num",num);
		model.addAttribute("cateName", cateName);
		model.addAttribute("supplierNo", supplierNo);
		model.addAttribute("brandId", brandId);
		return "/biz/intent/common/modalBarnd";
	}
	
	/**
	 * 意向合同详细信息
	 * @param id
	 * @param model
	 * @return
	 * @author yanghao
	 * @data 2014年5月23日
	 */
	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable Long id,HttpServletRequest request,RedirectAttributes redirectAttributes, Model model) {
		IntAssociated intAssociated = intAssociatedService.get(id);
	
		if(intAssociated ==null){
			addMessage(redirectAttributes, "意向不存在或已删除！");
			return null;
		}else{
			if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_LY)){
				return "redirect:/biz/intent/intAssociated/"+id+"/view";
			}else if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_ZY_DX)||intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_ZY_JX)){
				return "redirect:/biz/intent/intSelfociated/"+id+"/view";
			}else if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_GG)){
				return "redirect:/biz/intent/intAdvertociated/"+id+"/view";
			}else if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_ZP)){
				return "redirect:/biz/intent/intLeaseociated/"+id+"/view";
			}else if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_CX)){
				return "redirect:/biz/intent/intMarketociated/"+id+"/view";
			}
		}
		return null;
	}
	/**供应商确认是否合作
	 * 
	 * @param request
	 * @param response
	 * @param supplierAgree
	 * @throws Exception 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "{id}/updateSupplierConfirm")
	public void updateSupplierConfirm(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response,String supplierConfirm,RedirectAttributes redirectAttributes) throws Exception{
		//如果供应商同意合作，修改意向状态为供应商确认
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		StringBuilder sb = new StringBuilder();
		List<String> personIds = Lists.newArrayList();
		Map map = new HashMap();
		String navId = request.getParameter("navId");
		IntAssociated  intAssociated  = intAssociatedService.get(id);
		personIds.add(intAssociated.getCreateById());
		if(ContractUtils.SUPPLIER_AGREE_YES_.equals(supplierConfirm)){
			map.put("id", id);
			map.put("supplierConfirm", supplierConfirm);
			intAssociatedService.updateSupplierConfirm(map);
			List<Worker> listUser=workerServiceImpl.getUsersByPostID(ContractUtils.getProperty("postId").trim());
			for (Worker worker : listUser) {
				String[] user = new String[1];
				user[0]=worker.getWorkerNo();
				personIds.add(userService.queryUserIdsByIdentitys(user).get(0).toString());
			}
			if(intAssociated.getIntentType().equals(ContractUtils.CONTRATC_ATTRIBUTE_XUQ)&&StringUtil.isEmpty(intAssociated.getIsModifyContract())){
				HashMap<String, Object>  mapContract= Maps.newHashMap();
				mapContract.put("id",intAssociated.getContractId().toString());
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				Calendar cal=Calendar.getInstance();
				cal.setTime(intAssociated.getStartDate());
				cal.add(Calendar.DATE, -1);  //减1天
				mapContract.put("contractStatus", ContractUtils.CONTRACT_STATE_CLOSE);
				mapContract.put("terminateDate", formatter.format(cal.getTime()));
				mapContract.put("endDate", formatter.format(cal.getTime()));
				mapContract.put("contractAttribute", ContractUtils.CONTRATC_ATTRIBUTE_ZZ);
				mapContract.put("contractId", intAssociated.getContractId().toString());
				if(ContractUtils.CONTRACT_TYPE_LY.equals(intAssociated.getContractType())){
					contractServce.contractStatusUpdateById(mapContract);
					assbaseService.updateByContractID(mapContract);
					assiteseService.updateByContractID(mapContract);
				}else if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_ZP)){
					leaseService.updateContractStatusById(mapContract);
					leaseAssbaseitemsService.updateByContractID(mapContract);
					leaseAssitemsService.updateByContractID(mapContract);
				}else if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_CX)){
					marketService.updateContractStatusById(mapContract);
					marketAssbaseitemsService.updateByContractID(mapContract);
					marketAssitemsService.updateByContractID(mapContract);
				}else if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_GG)){
					advertService.updateContractStatusById(mapContract);
					advertAssitemsService.updateByContractID(mapContract);
				}else if(intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_ZY_DX)||
						intAssociated.getContractType().equals(ContractUtils.CONTRACT_TYPE_ZY_JX)){
					selfService.updateContractStatusById(mapContract);
					selfitemService.updateByContractID(mapContract);
				}
			}
			sendMessage(intAssociated, personIds,navId);
			sb.append("{\"success\":true}");
			out.write(sb.toString());
			out.flush();
			out.close();
		}else{
			//如果供应商不同意合作，发送待办给采购人，重新开始洽谈
			map.put("id", id);
			map.put("supplierConfirm", ContractUtils.SUPPLIER_AGREE_NO_);
			intAssociatedService.updateSupplierConfirm(map);
			//更改意向状态为“作废”
			map.put("id", id);
			map.put("status", ContractUtils.INTENT_STATUS_ZF);
			intAssociatedService.updateStatus(map);
			
			//发送待办给采购，重新开始洽谈
			List<String> targets = Lists.newArrayList();
			targets.add(intAssociated.getCreateById());
			
			TodoMessage toMessage = new TodoMessage();
			toMessage.setId(idGenerator.getUId());
			toMessage.setUrl(IntCommController.getReturnPage(intAssociated.getContractType(), navId, intAssociated.getId().toString()));   
			toMessage.setTitle("供应商不同意单号为"+intAssociated.getIntentNo()+"的相关条款，请重新开始洽谈！");
		
			toMessage.setTitleLink(IntCommController.getReturnPage(intAssociated.getContractType(), navId, intAssociated.getId().toString()));
			toMessage.setSource(MessageConstant.SOURCE_OSSP);
			toMessage.setMessageType(MessageConstant.TODO_TYPE_NOTIFY);
			toMessage.setContent("供应商不同意单号为"+intAssociated.getIntentNo()+"的相关条款，重新与供应商“"+intAssociated.getSupplierName()+"”开启 洽谈");
			toMessage.setContentTypeFlag(MessageConstant.TODO_CONTENT_TYPE_TEXT);
			toMessage.setCreateBy(UserUtils.getCurrentUserId().toString());
			toMessage.setCreateDate(new Date());
			toMessage.setPersonIds(targets);
			try {
				messageService.send(toMessage);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		sb.append("{\"success\":true}");
		out.write(sb.toString());
		out.flush();
		out.close();
	}
	/**
	 * 重新洽谈
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}/addFeedback")
	public String addFeedback(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response ,Model model){
		IntAssociated  intAssociated  = intAssociatedService.get(id);
		Long negotiationId = intAssociated.getNegotiationId();
		Assrecords assrecord = assrecordService.get(negotiationId);
		assrecord.setId(null);
		if(assrecord.getNegAssbaseitems() !=null){
			for(int i = 0;i<assrecord.getNegAssbaseitems().size();i++){
				NegAssbaseitems items = assrecord.getNegAssbaseitems().get(i);
				items.setId(null);
			}
		}
		if(assrecord.getNegAssitems() !=null){
			for(int i = 0;i<assrecord.getNegAssitems().size();i++){
				NegAssitems items = assrecord.getNegAssitems().get(i);
				items.setId(null);
			}
		}
		if(assrecord.getNegAssbrands() !=null){
			for(int i = 0;i<assrecord.getNegAssbrands().size();i++){
				NegAssbrands items = assrecord.getNegAssbrands().get(i);
				items.setId(null);
			}
		}
		if(assrecord.getNegAssshopsList() !=null){
			for(int i = 0;i<assrecord.getNegAssshopsList().size();i++){
				NegAssshops items = assrecord.getNegAssshopsList().get(i);
				items.setId(null);
			}
		}
		if(assrecord.getNegAssTempItemCodeList() !=null){
			for(int i = 0;i<assrecord.getNegAssTempItemCodeList().size();i++){
				NegAssTempItemCode items = assrecord.getNegAssTempItemCodeList().get(i);
				items.setId(null);
			}
		}
		assrecord.getAssinfo().setStatus("3");
		if(assrecord != null){
			model.addAttribute("assinfo", assrecord.getAssinfo());
			model.addAttribute("assrecords", assrecord);
		}
		model.addAttribute("contractType", intAssociated.getContractType());		//合同类型  联营
		model.addAttribute("negType", assrecord.getAssinfo().getNegotiationType());		//洽谈类型
		return resultPage("/biz/contract/assrecord/"+BuildEntity.getResultPage(intAssociated.getContractType(), "add"), model, request);
	}
	
	public Message sendMessage(IntAssociated intAssociated, List<String> personIds,String navId){
		//Assinfo assinfo = assinfoService.get(assinfoID);

		TodoMessage message = new TodoMessage();
		message.setTitle(UserUtils.getCurrentUserName()+"已确认单号为"+intAssociated.getIntentNo()+SysDictUtils.getValue("CONTRATC_ATTRIBUTE", intAssociated.getIntentType())+"的提报意向洽谈,请尽快处理。");
		message.setTitleLink(IntCommController.getReturnPage(intAssociated.getContractType(), navId, intAssociated.getId().toString()));
		message.setContent("");
		message.setContentTypeFlag(MessageConstant.TODO_CONTENT_TYPE_TEXT);
		message.setCreateBy(UserUtils.getCurrentUserId().toString());
		message.setCreateDate(new Date());
		message.setPersonIds(personIds);
		message.setMessageType(MessageConstant.TODO_TYPE_NOTIFY);
		Message msg = null;
		try {
			msg = mscServicec.send(message);
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return msg;
	}
	
	

	
	public static String getReturnPage(String contractType,String navId,String id){
		if(contractType.equals(ContractUtils.CONTRACT_TYPE_LY)){
			return "biz/intent/intAssociated/"+id+"/view?navId="+navId;
		}else if(contractType.equals(ContractUtils.CONTRACT_TYPE_ZP)){
			return "biz/intent/intLeaseociated/"+id+"/view?navId="+navId;
		}else if(contractType.equals(ContractUtils.CONTRACT_TYPE_CX)){
			return "biz/intent/intMarketociated/"+id+"/view?navId="+navId;
		}else if(contractType.equals(ContractUtils.CONTRACT_TYPE_GG)){
			return "biz/intent/intAdvertociated/"+id+"/view?navId="+navId;
		}else if(contractType.equals(ContractUtils.CONTRACT_TYPE_ZY_DX)|| contractType.equals(ContractUtils.CONTRACT_TYPE_ZY_JX)){
			return "biz/intent/intMarketociated/"+id+"/view?navId="+navId;
		}
		return null;
	}
	
	
	public static String getReturnContractPage(String contractType,String navId,String id){
		if(contractType.equals(ContractUtils.CONTRACT_TYPE_LY)){
			return "biz/contract/associated/"+id+"/view?navId="+navId;
		}else if(contractType.equals(ContractUtils.CONTRACT_TYPE_ZP)){
			return "biz/contract/leaseassociated/"+id+"/view?navId="+navId;
		}else if(contractType.equals(ContractUtils.CONTRACT_TYPE_CX)){
			return "biz/contract/marketassociated/"+id+"/view?navId="+navId;
		}else if(contractType.equals(ContractUtils.CONTRACT_TYPE_GG)){
			return "biz/contract/advertassociated/"+id+"/view?navId="+navId;
		}else if(contractType.equals(ContractUtils.CONTRACT_TYPE_ZY_DX)|| contractType.equals(ContractUtils.CONTRACT_TYPE_ZY_JX)){
			return "biz/contract/selfassociated/"+id+"/view?navId="+navId;
		}
		return null;
	}
	/**
	 * 终止意向提报
	 * @param id
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException 
	 */
	@RequestMapping(value = "{id}/closeIntent")
	public void closeIntent(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response ,Model model) throws IOException{
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		StringBuilder sb = new StringBuilder();
		IntAssociated  intAssociated  = intAssociatedService.get(id);
		Long negotiationId = intAssociated.getNegotiationId();
		Map map = new HashMap();
		//修改意向状态为作废
		map.put("id", id);
		map.put("status", ContractUtils.INTENT_STATUS_ZF);
		intAssociatedService.updateStatus(map);
		//意向提报终止后，将意向所属的洽谈记录状态修改为提交至采购人
		if(negotiationId!=null){
			Assrecords assrecords = assrecordService.get(negotiationId);
			Long infoId = assrecords.getBasicInfoId();
			map.put("id", infoId);
			map.put("status", ContractUtils.NEG_STATUS_GYS);
			assinfoService.updateStatus(map);
		}
		sb.append("{\"success\":true}");
		out.write(sb.toString());
		out.flush();
		out.close();
		
	}
	
	/**
	 * 取得招商分类
	 * @param cmcCate	
	 * @param icmCateService
	 * @return
	 * @throws Exception
	 * @author yanghao
	 * @data 2014年6月10日
	 */
	public static String getCateName(CMCate cmcCate, ICMCateService icmCateService) throws Exception{
		String cateName ="";
		if(cmcCate.getParent() != null){			
			if(cmcCate.getParent().getParentId() != null){		
				CMCate cate2 = cmcCate.getParent().getParent();
				cateName += cate2.getCateName()+" - ";			//大类名称
			}
			cateName += cmcCate.getParent().getCateName()+" - ";		//中类名称
		}
		cateName += cmcCate.getCateName();		//小类名称
		return cateName;
	}
	
	/**
	 * 删除附件
	* @Title: removeAtt
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param request
	* @param @param response
	* @param @param model    设定文件
	* @return void    返回类型
	* @throws
	 */
	@RequestMapping(value = "removeAtt")
	public void removeAtt(HttpServletRequest request,HttpServletResponse response,Model model){
		String id =request.getParameter("id");
		attServie.remove(Long.parseLong(id));
	}
	
	/**
	 * 修改附件
	* @Title: editAtt
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param request
	* @param @param response
	* @param @param model    设定文件
	* @return void    返回类型
	* @throws
	 */
	@RequestMapping(value = "editAtt")
	public void editAtt(HttpServletRequest request,HttpServletResponse response,Model model){
		String id =request.getParameter("id");
		String confim =request.getParameter("confim");
		IntAttachment att=attServie.get(Long.parseLong(id));
		att.setIsContrfim(confim);
		attServie.update(att);
	}
}
